import unittest
from functionBinding import functionBinding
from functionDef import functionDef
from application import application
from Return import Return
from builtin import *
from varRef import varRef
from jsobject import *
from interpreter import interpreter
from endvals import *
from objectOps import *
from sequence import *
from conditionals import *
from sequence import *
from varBinding import *
from varMut import *

class TestMisc(unittest.TestCase):
	def testControlFlowWhile(self):
		print "\nTesting While:",
		p = varBinding("i", integer(10), 
									sequence([
														whileloop(application(">", [varRef("i"),integer(0)]),
																 sequence([
																 				varMut(stringval("i"), application("-", [varRef("i"), integer(1)]) ), 
																 				application("print", [varRef("i")])])
																 			),
														 Return(varRef("i"))
														])
										)
		i = interpreter(p)	
		assert i.execute() == 0, "testControlFlowWhile Failed! (see above text for how)"
		print "\nShould See   : 9 8 7 6 5 4 3 2 1 0" 
		
#--	------------------------------------------------------------------
		
	def testControlFlowIf(self):
		p = varBinding("i", integer(1), 
											sequence([
																	ifthen(application("==",[varRef("i"), integer(1)]),
																		 			Return(integer(1)),
																		 			Return(integer(2)) 
																		 		)
																])
									  )
		i = interpreter(p)
		assert i.execute() == 1, "testControlFlowIf Failed! Should have taken first branch but did not"
		
		p = varBinding("i", integer(2), 
											sequence([
																	ifthen(application("==",[varRef("i"), integer(1)]),
																		 			Return(integer(1)),
																		 			Return(integer(2)) 
																		 		)
																])
									  )
		i = interpreter(p)
		assert i.execute() == 2, "testControlFlowIf Failed! Should have taken second branch but did not"
		
#--	------------------------------------------------------------------
		
	def testStringPlus(self):
		p = varBinding("newString", application("+", [stringval("one "), stringval("string")]),
										Return(application("string=?", [varRef("newString"), stringval("one string")]))
									  )
		i = interpreter(p)	
		assert i.execute() == True, "testStringPlus Failed!"
		
if __name__ == '__main__':
    unittest.main()
